def f(query,sub,ins):
    res = []
    if query not in sub and query not in ins:
        return res
    if query in ins:
        res += ins[query]
    if query in sub:
        subclass = sub[query]
        for key in subclass:
            res += f(key,sub,ins)
    return res

n = int(input())
sub,ins = {},{}
for _ in range(n):
    infor = input().split()
    if infor[1] == 'instanceOf':
        if infor[2] in ins:
            ins[infor[2]].append(infor[0])
        else:
            ins[infor[2]] = [infor[0]]
    else:
        if infor[2] in sub:
            sub[infor[2]].append(infor[0])
        else:
            sub[infor[2]] = [infor[0]]
query = input()
res = f(query,sub,ins)
res = list(set(res))
if len(res)==0:
    print('empty')
else:
    res.sort()
    print(' '.join(res))







